﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qiang.Data;

namespace Qiang.Styles
{
    public abstract class Layer : ILayer
    {

        #region 初始化

        public Layer(IDataset dataset)
        {
            Configure(dataset);
        }

        void Configure(IDataset dataset)
        {
            this._dataset = dataset;
            this._selection = new Selection(this._dataset);
        }

        #endregion


        #region 事件

        /// <summary>
        /// 数据内容改变事件
        /// </summary>
        public event EventHandler DatasetContentChanged;

        /// <summary>
        /// 图层内容改变事件
        /// </summary>
        public event EventHandler LayerContentChanged;

        #endregion


        #region 属性

        private IDataset _dataset;

        /// <summary>
        /// 数据集
        /// </summary>
        public IDataset Dataset
        {
            get { return _dataset; }
            set 
            {
                if (_dataset == value) return;
                _dataset = value; 
            }
        }
                      

        /// <summary>
        /// 范围
        /// </summary>
        public IExtent Extent
        {
            get 
            {
                return this.ComputeExtent();               
            }
        }

        /// <summary>
        /// 图层所选内容
        /// </summary>
        private ISelection _selection;

        /// <summary>
        /// 图层所选内容
        /// </summary>
        public ISelection Selection
        {
            get
            {
                return _selection;
            }
        }

        private bool _selectable;

        /// <summary>
        /// 是否可选
        /// </summary>
        public bool Selectable
        {
            get { return _selectable; }
            set { _selectable = value; }
        }


        private bool _visible;

        /// <summary>
        /// 图层是否可见
        /// </summary>
        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }




        #endregion


        #region 方法

        public virtual void Select(IExtent ext)
        { 

        }


        public virtual void Select(IExtent ext, SelectionMode selectMode)
        { }

        
        public virtual void ClearSelected()
        { }

        public virtual void RemoveSelected()
        { }

        #endregion


        #region 受保护的方法

        /// <summary>
        /// 获取图层的范围
        /// </summary>
        /// <returns></returns>
        protected virtual IExtent ComputeExtent()
        {
            return Qiang.Data.Extent.Empty();
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void OnDatasetContentChanged()
        {
            if (DatasetContentChanged != null)
                DatasetContentChanged(this, new EventArgs());
        }

        protected virtual void OnLayerContentChanged()
        {
            if (LayerContentChanged != null)
                LayerContentChanged(this, new EventArgs());
        }

        #endregion


        #region 释放资源

        private bool _disposed;

        /// <summary>
        /// 指示对象是否已销毁
        /// </summary>
        public bool Disposed
        {
            get { return _disposed; }
            set { _disposed = value; }
        }

        /// <summary>
        /// 释放所有托管与非托管资源
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (this._disposed == false)
            {
                if (disposing)
                {
                    //释放托管资源


                }
                //释放非托管资源



            }
            this._disposed = true;
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~Layer()
        {
            this.Dispose(false); //释放非托管
        }

        #endregion

    }
}
